const Flow = require("../Flow");
const Player = require("../Player");
const Card = require("../Card");

const PLAYER_LIST = ["gaubee", "bangeel"].map(username => new Player({
	nickname: username
}));

const FLOW = new Flow(PLAYER_LIST);

// test other player
try {
	const other_player = new Player({
		nickname: "zz"
	});
	FLOW.playerSubmitRes(other_player, {});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "此玩家不在本场游戏中", "其它玩家不可在此游戏中游戏")
}

const player_0 = PLAYER_LIST[0];
const player_1 = PLAYER_LIST[1];

// test player submit result
try {
	FLOW.playerSubmitRes(player_0, {
		top: []
	});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "提交的纸牌排列结果有误", "提交的纸牌必须是3-5-5的排列")
}

player_0.removeCards(player_0.card_list);
player_0.addCards("红心_J 方块_7 黑桃_10 红心_10 红心_Q 黑桃_9 梅花_4 黑桃_Q 黑桃_2 梅花_3 梅花_J 方块_2 红心_K".split(" ").map(txt => Card.getIns(txt)));

player_1.removeCards(player_1.card_list);
player_1.addCards("黑桃_5 红心_9 黑桃_Q 红心_10 红心_Q 黑桃_8 红心_J 方块_6 方块_1 红心_8 红心_5 红心_1 方块_3".split(" ").map(txt => Card.getIns(txt)));
// test player submit result
try {
	FLOW.playerSubmitRes(player_0, {
		top: "红心_J 方块_7 黑桃_10".split(" "),
		mid: "红心_10 红心_Q 黑桃_9 梅花_4 黑桃_Q".split(" "),
		btm: "黑桃_2 梅花_3 梅花_J 方块_2 红心_Q".split(" "),
	});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "存在作弊情况，提交了玩家不拥有的纸牌", "提交的纸牌必须是玩家手上的牌")
}

try { // 检查是否倒水
	FLOW.playerSubmitRes(player_0, {
		top: "梅花_J 红心_J 黑桃_9".split(" "),
		mid: "方块_2 黑桃_2 梅花_3 梅花_4 方块_7".split(" "),
		btm: "红心_10 黑桃_10 红心_Q 黑桃_Q 红心_K".split(" "),
	});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "头、中两墩牌发生倒水情况", "提交的纸牌不可倒水")
}

try { // 检查是否倒水
	FLOW.playerSubmitRes(player_0, {
		top: "方块_2 黑桃_2 黑桃_9".split(" "),
		mid: "梅花_J 红心_J 红心_Q 黑桃_Q 方块_7".split(" "),
		btm: "红心_10 黑桃_10 梅花_3 梅花_4 红心_K".split(" "),
	});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "中、尾两墩牌发生倒水情况", "提交的纸牌不可倒水")
}

// 正常提交获取结果提交
FLOW.playerSubmitRes(player_0, {
	top: "方块_2 黑桃_2 黑桃_9".split(" "),
	mid: "梅花_J 红心_J 梅花_3 梅花_4 方块_7".split(" "),
	btm: "红心_10 黑桃_10 红心_Q 黑桃_Q 红心_K".split(" "),
});
try { // 检查是否重复提交
	FLOW.playerSubmitRes(player_0, {
		top: "方块_2 黑桃_2 黑桃_9".split(" "),
		mid: "梅花_J 红心_J 梅花_3 梅花_4 方块_7".split(" "),
		btm: "红心_10 黑桃_10 红心_Q 黑桃_Q 红心_K".split(" "),
	});
	throw "TEST ERROR";
} catch (e) {
	console.assert(e.message === "玩家已经提交过，不可重复提交", "不可重复提交")
}

FLOW.playerSubmitRes(player_1, {
	top: "黑桃_5 黑桃_Q 方块_3".split(" "),
	mid: "方块_1 红心_1 红心_5 黑桃_8 方块_6".split(" "),
	btm: "红心_8 红心_J 红心_Q 红心_10 红心_9".split(" "),
});
console.log(JSON.stringify(FLOW.calculateResults(), null, 4) , `[
    {
        "player_id": "gaubee",
        "one_vs_one_list": [
            {
                "top": 1,
                "mid": -1,
                "btm": -1,
                "multiple": 1,
                "extra": 0
            }
        ],
        "multiple": 1,
        "extra": 0,
        "total": -1
    },
    {
        "player_id": "bangeel",
        "one_vs_one_list": [
            {
                "top": -1,
                "mid": 1,
                "btm": 1,
                "multiple": 1,
                "extra": 0
            }
        ],
        "multiple": 1,
        "extra": 0,
        "total": 1
    }
]`);